package overall.manager
{
	import morn.core.components.Component;
	import morn.core.components.Image;
	import morn.core.components.Label;
	import morn.core.components.List;
	import morn.core.components.View;
	import morn.core.handlers.Handler;
	
	import overall.model.PlayerInfo;
	import view.room.MjRoom_Table;
	
	import uiPackage.mjRoom.MjRoom_TableUI;

	public class ListManager
	{
		public function ListManager()
		{
		}
		static private var _instance:ListManager;
		static public function get instance():ListManager{
			_instance=_instance||new ListManager;
			return _instance;
		}
		
		public function setDataToList(list:List,obj:*,str:String):void
		{
			switch(str){
				case "userList":
					_ayxUserList(list,obj);
					break;
				case "tableList":
					_ayxTableList(list,obj);
					break;
			}
		}
		private function _ayxUserList(list:List,data:Vector.<PlayerInfo>):void
		{
			var arr:Array=new Array;
			for(var i:int=0;i<data.length;i++){
				arr.push(data[i]);
			}
			list.dataSource=arr;
			list.renderHandler=new Handler(_renderUserList);
			function _renderUserList(item:Component,indx:int):void
			{
				if(indx<list.length){
					item["avatarImg"].url=list.array[indx].avatarUrl;
					item["nameText"].text=list.array[indx].name;
				}
			}
		}
		private function _ayxTableList(list:List,data:Vector.<PlayerInfo>):void
		{
            var arr:Array=new Array;
			for(var i:int=0;i<list.length;i++){
				arr[i]=new Object;
				arr[i]["userImg0"]="png.mjRoom.mj_room_table_nobody";
				arr[i]["userImg1"]="png.mjRoom.mj_room_table_nobody";
				arr[i]["userImg2"]="png.mjRoom.mj_room_table_nobody";
				arr[i]["userImg3"]="png.mjRoom.mj_room_table_nobody";
				arr[i]["userName0"]="";
				arr[i]["userName1"]="";
				arr[i]["userName2"]="";
				arr[i]["userName3"]="";
				arr[i]["status0"]=0;
				arr[i]["status1"]=0;
				arr[i]["status2"]=0;
				arr[i]["status3"]=0;
			}
			for(i=0;i<data.length;i++){
				var n:int=data[i].tableId;
				var m:int=data[i].post;
				if(n<arr.length&&n>-1){
					arr[n]["userImg"+String(m)]=data[i].avatarUrl;
					arr[n]["userName"+String(m)]=data[i].name;
					arr[n]["status"+String(m)]=data[i].status;
				}
			}
			list.dataSource=arr;
			list.renderHandler=new Handler(_renderUserList);
			function _renderUserList(item:Component,indx:int):void
			{
				if(indx<list.length){
					MjRoom_Table(item).id=indx;
					MjRoom_Table(item).setTableData(list.array[indx]);
				}
			}
		}
	}
}